import BST from './binarySearchTree'
export default {
  created() {
    this.createBST();
    this.preorder();
    this.inorder();
    this.lastorder();
    this.searchNode(10);
    this.findMin();
    this.findMax();
    this.compareBST();
    this.judgeInvalidBST();
    this.delete();
  },
  data() {
    return {
      bst: null,
      invalidBst: {
        data: 10,
        left: {
          data: 5
        },
        right: {
          data: 15,
          left: {
            data: 6
          },
          right: {
            data: 20
          }
        }
      }
    };
  },
  methods: {
    createBST() {
      const bst = this.bst = new BST();
      bst.insert(5).insert(10).insert(2).insert(3).insert(9).insert(12).insert(6);

      console.log(bst.getBst());
    },
    compareBST() {
      const testBst = (new BST()).insert(5).insert(10).insert(2);
      console.log('比较两棵二叉树是否完全相同-------------------');
      console.log(BST.isSameTree(this.bst.root, testBst.root));
    },
    delete() {
      const data = 10;
      this.bst.delete(data);
      console.log(`删除值为 ${data} 后的二叉树-----------------`);
      console.log(this.bst);
    },
    judgeInvalidBST() {
      console.log('判断二叉树是否合法-------------------------');
      console.log(BST.isValidBST(this.invalidBst));
    },
    searchNode(data) {
      console.log(`BST 查询数据 ${data} 对应的节点-------------`);
      console.log(this.bst.search(data));
    },
    findMin() {
      console.log(`二叉树中最小节点为 -------------------------`);
      console.log(this.bst.findMin());
    },
    findMax() {
      console.log(`二叉树中最大节点为 -------------------------`);
      console.log(this.bst.findMax());
    },
    preorder() {
      console.log('前序遍历 ----------------------------------');
      const root = this.bst.root;
      this.bst.preorder(root);
    },
    inorder() {
      console.log('中序遍历 ----------------------------------');
      const root = this.bst.root;
      this.bst.inorder(root);
    },
    lastorder() {
      console.log('后序遍历 ----------------------------------');
      const root = this.bst.root;
      this.bst.lastorder(root);
    }
  }
}
